#!/bin/bash 
# 
# elasticsearch 安装
#
# 需要使用file.sh 的 $elasticsearchDownUrl

# 安装elasticsearch
echo '开始安装es'
cd ~
# 下载 ElasticSearch 7.10.1
wget $elasticsearchDownUrl
# 安装 tar （适配 openEuler 22.03）
yum -y install tar
tar -zxvf elasticsearch-7.10.1-linux-x86_64.tar.gz
# 创建elsearch用户组及elsearch用户
espassword=$RANDOM$RANDOM$RANDOM$RANDOM
groupadd elsearch
useradd elsearch -g elsearch -p $espassword
# 更改elasticsearch文件夹及内部文件的所属用户及组为elsearch:elsearch
chown -R elsearch:elsearch elasticsearch-7.10.1
# 将其转移到/mnt/elasticsearch
mkdir /mnt
mv elasticsearch-7.10.1 /mnt/elasticsearch
# 修改es配置文件
sed -i "s#^-Xms256m#-Xms128m#g" /mnt/elasticsearch/config/jvm.options
sed -i "s#^-Xmx256m#-Xmx128m#g" /mnt/elasticsearch/config/jvm.options
# 修改 elasticsearch 执行文件
sed -i '16i export JAVA_RELATIVE=/mnt/elasticsearch/jdk/ \
export JAVA_HOME=/mnt/elasticsearch/jdk/ \
export PATH=$JAVA_HOME/bin:$PATH \
echo JAVA_RELATIVE : $JAVA_RELATIVE \
#添加jdk判断 \
if [ -x "$JAVA_HOME/bin/java" ]; then \
        JAVA=$JAVA_RELATIVE \
else \
        JAVA=`which java` \
fi' /mnt/elasticsearch/bin/elasticsearch
# 修改 elasticsearch.yml 配置文件
sed -i '23s/#//g' /mnt/elasticsearch/config/elasticsearch.yml
sed -i '55s/#network.host: 192.168.0.1/network.host: 0.0.0.0/g' /mnt/elasticsearch/config/elasticsearch.yml
sed -i '72s/#//g' /mnt/elasticsearch/config/elasticsearch.yml
sed -i '72s/, "node-2"//g' /mnt/elasticsearch/config/elasticsearch.yml
# 新建 startup.sh 启动文件
touch /mnt/elasticsearch/startup.sh
echo 'ulimit -n 65536'>>/mnt/elasticsearch/startup.sh
echo 'sysctl -w vm.max_map_count=262144'>>/mnt/elasticsearch/startup.sh
echo 'su - elsearch -c /mnt/elasticsearch/bin/elasticsearch'>>/mnt/elasticsearch/startup.sh
echo 'echo "start up finish"'>>/mnt/elasticsearch/startup.sh
# 加入开机启动文件
echo '/mnt/elasticsearch/startup.sh'>>/etc/rc.d/rc.local
# 赋予可执行权限
chmod +x /mnt/elasticsearch/startup.sh
chmod -R 777 /mnt/elasticsearch/startup.sh
chmod +x /etc/rc.d/rc.local
## 启动
nohup /mnt/elasticsearch/startup.sh >> /root/es.log 2>&1 &